home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 July / CD 3 / redhat-6.2.iso / RedHat / instimage / usr / lib / anaconda / iutil.py < prev    next >
Encoding:
Python Source  |  2000-03-08  |  6.4 KB  |  294 lines

  1.  
  2. import types, os, sys, isys, select, string, stat, signal
  3.  
  4. def getArch ():
  5.     arch = os.uname ()[4]
  6.     if (len (arch) == 4 and arch[0] == 'i' and
  7.         arch[2:4] == "86"):
  8.         arch = "i386"
  9.  
  10.     if arch == "sparc64":
  11.         arch = "sparc"
  12.  
  13.     return arch
  14.  
  15. def getfd(filespec, readOnly = 0):
  16.     if type(filespec) == types.IntType:
  17.     return filespec
  18.     if filespec == None:
  19.     filespec = "/dev/null"
  20.  
  21.     flags = os.O_RDWR | os.O_CREAT
  22.     if (readOnly):
  23.     flags = os.O_RDONLY
  24.     return os.open(filespec, flags)
  25.  
  26. def execWithRedirect(command, argv, stdin = 0, stdout = 1, stderr = 2,    
  27.              searchPath = 0, root = '/', newPgrp = 0,
  28.              ignoreTermSigs = 0):
  29.     stdin = getfd(stdin)
  30.     if stdout == stderr:
  31.     stdout = getfd(stdout)
  32.     stderr = stdout
  33.     else:
  34.     stdout = getfd(stdout)
  35.     stderr = getfd(stderr)
  36.  
  37.     if not os.access (root + command, os.X_OK):
  38.         if not os.access (command, os.X_OK):
  39.             raise RuntimeError, command + " can not be run"
  40.         else:
  41.             root = ""
  42.  
  43.     childpid = os.fork()
  44.     if (not childpid):
  45.         if (root and root != '/'): isys.chroot (root)
  46.  
  47.     if ignoreTermSigs:
  48.         signal.signal(signal.SIGTSTP, signal.SIG_IGN)
  49.         signal.signal(signal.SIGINT, signal.SIG_IGN)
  50.  
  51.     if type(stdin) == type("a"):
  52.         stdin == os.open(stdin, os.O_RDONLY)
  53.     if type(stdout) == type("a"):
  54.         stdout == os.open(stdout, os.O_RDWR)
  55.     if type(stderr) == type("a"):
  56.         stderr = os.open(stderr, os.O_RDWR)
  57.  
  58.     if stdin != 0:
  59.         os.dup2(stdin, 0)
  60.         os.close(stdin)
  61.     if stdout != 1:
  62.         os.dup2(stdout, 1)
  63.         if stdout != stderr:
  64.         os.close(stdout)
  65.     if stderr != 2:
  66.         os.dup2(stderr, 2)
  67.         os.close(stderr)
  68.  
  69.     if (searchPath):
  70.         os.execvp(command, argv)
  71.     else:
  72.         os.execv(command, argv)
  73.  
  74.     sys.exit(1)
  75.  
  76.     if newPgrp:
  77.     os.setpgid(childpid, childpid)
  78.     oldPgrp = os.tcgetpgrp(0)
  79.     os.tcsetpgrp(0, childpid)
  80.  
  81.     (pid, status) = os.waitpid(childpid, 0)
  82.  
  83.     if newPgrp:
  84.     os.tcsetpgrp(0, oldPgrp)
  85.  
  86.     return status
  87.  
  88. def execWithCapture(command, argv, searchPath = 0, root = '/', stdin = 0):
  89.  
  90.     if not os.access (root + command, os.X_OK):
  91.         if not os.access (command, os.X_OK):
  92.             raise RuntimeError, command + " can not be run"
  93.         else:
  94.             root = ""
  95.  
  96.     (read, write) = os.pipe()
  97.  
  98.     childpid = os.fork()
  99.     if (not childpid):
  100.         if (root and root != '/'): isys.chroot (root)
  101.     os.dup2(write, 1)
  102.  
  103.     if stdin:
  104.         os.dup2(stdin, 0)
  105.         os.close(stdin)
  106.  
  107.     if (searchPath):
  108.         os.execvp(command, argv)
  109.     else:
  110.         os.execv(command, argv)
  111.  
  112.     sys.exit(1)
  113.  
  114.     os.close(write)
  115.  
  116.     rc = ""
  117.     s = "1"
  118.     while (s):
  119.     select.select([read], [], [])
  120.     s = os.read(read, 1000)
  121.     rc = rc + s
  122.  
  123.     os.close(read)
  124.  
  125.     os.waitpid(childpid, 0)
  126.  
  127.     return rc
  128.  
  129. def copyFile(source, to):
  130.     f = os.open(source, os.O_RDONLY)
  131.     t = os.open(to, os.O_RDWR | os.O_TRUNC | os.O_CREAT)
  132.  
  133.     count = os.read(f, 16384)
  134.     while (count):
  135.     os.write(t, count)
  136.     count = os.read(f, 16384)
  137.     
  138.     os.close(f)
  139.     os.close(t)
  140.  
  141. def memInstalled():
  142.     f = open("/proc/meminfo", "r")
  143.     mem = f.readlines()[1]
  144.     del f
  145.  
  146.     # patch from hjl
  147.     fields = string.split(mem)
  148.     try:
  149.        mem = int(fields[1]) / 1024
  150.     except:
  151.         mem = 2097151
  152.  
  153.     return mem
  154.  
  155. # this is a mkdir that won't fail if a directory already exists and will
  156. # happily make all of the directories leading up to it. 
  157. def mkdirChain(dir):
  158.     if (os.path.isdir(dir)): return
  159.     elements = string.splitfields(dir, "/")
  160.  
  161.     if (len(elements[0])):
  162.     which = 1
  163.     path = elements[0] 
  164.     else:
  165.     which = 2
  166.     path = "/" + elements[1]
  167.  
  168.     if (not os.path.isdir(path)): 
  169.     os.mkdir(path, 0755)
  170.  
  171.     while (which < len(elements)):
  172.     path = path + "/" + elements[which]
  173.     which = which + 1
  174.     
  175.     if (not os.path.isdir(path)): 
  176.         os.mkdir(path, 0755)
  177.  
  178. #
  179. # get default runlevel - only for use in reconfig mode
  180. #
  181. def getDefaultRunlevel ():
  182.     inittab = open ('/etc/inittab', 'r')
  183.     lines = inittab.readlines ()
  184.     inittab.close ()
  185.     for line in lines:
  186.         if len (line) > 3 and line[:3] == "id:":
  187.             fields = string.split (line, ':')
  188.             return fields[1]
  189.  
  190.     return None
  191.  
  192. def makerelname(relpath, filename):
  193.     if relpath != '':
  194.         return relpath+'/'+filename
  195.     else:
  196.         return filename
  197.     
  198.     
  199. def findtz(basepath, relpath):
  200.     tzdata = []
  201.     for n in os.listdir(basepath+'/'+relpath):
  202.         timezone = makerelname(relpath, n)
  203.         if relpath != '':
  204.             timezone = relpath+'/'+n
  205.         else:
  206.             timezone = n
  207.             
  208.         filestat = os.lstat(basepath+'/'+timezone)
  209.         [filemode] = filestat[:1]
  210.         
  211.         if (not (stat.S_ISLNK(filemode) or
  212.                  stat.S_ISREG(filemode) or
  213.                  stat.S_ISDIR(filemode))):
  214.             continue
  215.         elif n[:1] >= 'A' and n[:1] <= 'Z':
  216.             if stat.S_ISDIR(filemode):
  217.                 tmptzdata = findtz(basepath, timezone)
  218.             else:
  219.                 tmptzdata = [timezone]
  220.                     
  221.         for m in tmptzdata:
  222.             if tzdata == []:
  223.                 tzdata = [m]
  224.             else:
  225.                 tzdata.append(m)
  226.  
  227.         tzdata.sort()
  228.                             
  229.     return tzdata
  230.  
  231. def rmrf (path):
  232.     # this is only the very simple case.
  233.     files = os.listdir (path)
  234.     for file in files:
  235.         if os.path.isdir(path + '/' + file):
  236.             rmrf (path + '/' + file)
  237.         else:
  238.             os.unlink (path + '/' + file)
  239.     os.rmdir (path)
  240.  
  241. def validUser (user):
  242.     if len (user) > 8:
  243.         return 0
  244.     
  245.     if not user[0] in string.letters:
  246.         return 0
  247.  
  248.     for letter in user:
  249.         if (letter == ':'
  250.             or letter == ','
  251.             or letter == '\n'
  252.             or ord (letter) < 33):
  253.             return 0
  254.  
  255.     return 1
  256.  
  257. def setClock (root):
  258.     # eeeeew, inline shell. ;)
  259.     args = ("bash", "-c", """
  260. if [ -f /etc/sysconfig/clock ]; then
  261.    . /etc/sysconfig/clock
  262.    
  263.    # convert old style clock config to new values
  264.    if [ "${CLOCKMODE}" = "GMT" ]; then
  265.       UTC=true
  266.    elif [ "${CLOCKMODE}" = "ARC" ]; then
  267.       ARC=true
  268.    fi
  269. fi
  270.  
  271. CLOCKFLAGS="--hctosys"
  272.  
  273. case "$UTC" in
  274.    yes|true)
  275.     CLOCKFLAGS="$CLOCKFLAGS -u";
  276.      ;;
  277. esac
  278.  
  279. case "$ARC" in
  280.      yes|true)
  281.         CLOCKFLAGS="$CLOCKFLAGS -A";
  282.      ;;
  283. esac
  284. case "$SRM" in
  285.      yes|true)
  286.         CLOCKFLAGS="$CLOCKFLAGS -S";
  287.      ;;
  288. esac
  289. /sbin/hwclock $CLOCKFLAGS
  290. """)
  291.     execWithRedirect('/bin/sh', args, stdin = None,
  292.                      stdout = None, stderr = None,
  293.                      root = root)
  294.